package com.lucian.ge.util.wxpay.sdk;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * Created by Lucicol on 2019/7/23.
 */
@Component
//@ConfigurationProperties(prefix = "file")
public class MyConfig extends WXPayConfig {
    private byte[] certData;

    private static String appId;
    private static String appSecret;
    private static String apiKey;
    private static String mchId;

    @Value("${wechat.appid}")
    public void setappId(String appId) {
        this.appId = appId;
    }
    @Value("${wechat.appsecret}")
    public void setappSecret(String appSecret) {
        this.appSecret = appSecret;
    }
    @Value("${wechat.apikey}")
    public void setapiKey(String apiKey) {
        this.apiKey = apiKey;
    }
    @Value("${wechat.mchid}")
    public void setmchId(String mchId) {
        this.mchId = mchId;
    }

    public MyConfig() throws Exception {
//        String certPath = "/path/to/apiclient_cert.p12";
//        File file = new File(certPath);
//        InputStream certStream = new FileInputStream(file);
//        this.certData = new byte[(int) file.length()];
//        certStream.read(this.certData);
//        certStream.close();
    }

    public IWXPayDomain getWXPayDomain() {
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {
            public void report(String domain, long elapsedTimeMillis, Exception ex) {
            }
            public DomainInfo getDomain(WXPayConfig config) {
                return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);
            }
        };
        return iwxPayDomain;

    }

    public String getAppID() {
//        System.out.println("appId"+appId);
        return appId;
//        return "wxa13b77d438fbf138";
    }

    public String getMchID() {
        return mchId;
    }

    public String getKey() {
        return apiKey;
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

}
